#pragma once

#include <iostream>
#include <vector>

using namespace std;

namespace lcl
{

	template<class T>
	struct less
	{
		bool operator()(const T& x1, const T& x2)
		{
			return x1 < x2;
		}
	};

	template<class T>
	struct greater
	{
		bool operator()(const T& x1, const T& x2)
		{
			return x1 > x2;
		}
	};

	template<class T, class Container = vector<T>, class Compare = less<T>>
	//template<class T, class Container = vector<T>>

	class my_priority_queue
	{
	public:
		void AdjustUp(int child)
		{
			Compare com;
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				//if (_con[child] > _con[parent])
				if(com(_con[parent], _con[child]))
				{
					swap(_con[child], _con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void AdjustDown(int parent)
		{
			Compare com;
			int child = 2 * parent + 1;
			while (child < _con.size())
			{
				//if (child + 1 < _con.size() && _con[child + 1] > _con[child])
				if (child + 1 < _con.size() && com(_con[child], _con[child + 1]))
				{
					child = child + 1;
				}
				//if (_con[child] > _con[parent])
				if (com(_con[parent], _con[child]))
				{
					swap(_con[child], _con[parent]);
					parent = child;
					child = 2 * parent + 1;
				}
				else
				{
					break;
				}
			}
		}
		void push(const T& val)
		{
			_con.push_back(val);
			AdjustUp(_con.size() - 1);
		}

		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();

			AdjustDown(0);
		}

		T& top(){return _con[0];}
		size_t size(){return _con.size();}
		bool empty(){return _con.empty();}
	private:
		Container _con;
	};

	void test()
	{
		//my_priority_queue<int> mq;
		my_priority_queue<int, vector<int>, class greater<int>> mq;
		mq.push(1);
		mq.push(3);
		mq.push(20);
		mq.push(87);
		mq.push(9);
		mq.push(2);

		while (!mq.empty())
		{
			cout << mq.top() << " ";
			mq.pop();
		}
		/*for (size_t i = 0; i < mq.size(); ++i)
		{
			cout << mq.top() << " ";
			mq.pop();
		}*/
	}
	
}


